package pers.cj.pdos.controller;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.cj.pdos.controller.base.BaseController;
import pers.cj.pdos.controller.entity.ResponseData;
import pers.cj.pdos.controller.entity.ResponseEntity;
import pers.cj.pdos.controller.entity.ResponseList;
import pers.cj.pdos.entity.Company;
import pers.cj.pdos.entity.Unit;
import pers.cj.pdos.service.IUnitService;
import pers.cj.pdos.utils.DataUtil;
import pers.cj.pdos.utils.UuidUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Controller
@RequestMapping("/goods")
public class UnitController extends BaseController {
    @Autowired
    private IUnitService unitService;

    @RequiresPermissions("unit:query")
    @RequestMapping("unit")
    public String unit() {
        return "goods/unit";
    }


    @RequiresPermissions("unit:insert")
    @RequestMapping("unitNew")
    public String unitNew() {
        return "goods/unitNew";
    }

    @RequiresPermissions("unit:insert")
    @RequestMapping("unitEdit")
    public String unitEdit(Model model,String id) {
        model.addAttribute("id",id);
        return "goods/unitEdit";
    }

    @ResponseBody
    @RequiresPermissions("unit:query")
    @RequestMapping("/unit/getUnits")
    public ResponseList getUnits(int page, int limit, String searchContent) {
        Company company = getCompany();

        List<Unit> list = unitService.listPage(page, limit, company.getId(), searchContent,null);

        //返回数据
        return ResponseList.success(unitService.count(company.getId(),searchContent,null), list);
    }

    @ResponseBody
    @RequiresPermissions("unit:insert")
    @RequestMapping("/unit/add")
    public ResponseEntity add(@RequestBody Unit unit) {
        Company company = getCompany();
        if (unitService.checkName(unit.getName(),company.getId())){
            return new ResponseEntity(2,"单位名称已存在");
        }
        DataUtil.processEntity(company,unit);
        unitService.insert(unit);
        return ResponseEntity.success();
    }

    @ResponseBody
    @RequiresPermissions("unit:delete")
    @RequestMapping("/unit/delete")
    public ResponseEntity delete(@RequestBody Unit unit) {
        if (unitService.check(unit.getId())) {
            return ResponseEntity.fail("已被使用无法删除");
        }
        DataUtil.processEntity(false,getCompany(),unit);
        unitService.remove(unit);
        return ResponseEntity.success();
    }

    @ResponseBody
    @RequiresPermissions("unit:insert")
    @RequestMapping("/unit/update")
    public ResponseData update(@RequestBody Unit unit) {
        Company company = getCompany();

        Unit temp = unitService.getById(unit.getId());
        if (temp == null){
            return ResponseData.fail("id不正确");
        }

        if (!temp.getName().equals(unit.getName()) && unitService.checkName(unit.getName(),company.getId())){
            return ResponseData.fail("单位名称已存在");
        }

        if (unit.getStatus()!=1 && unitService.check(unit.getId())){
            //单位被停用，且正在被使用，则无法更新
            return ResponseData.fail("已被使用，无法停用");
        }

        if (unit.getPoint() && unitService.checkSn(unit.getId())) {
            return ResponseData.fail("已被启用序列号的商品使用，无法开启小数");
        }

        DataUtil.processEntity(false,company,unit);
        unitService.update(unit);
        return ResponseData.success(unit);
    }

    @ResponseBody
    @RequiresPermissions("unit:delete")
    @RequestMapping("/unit/deleteBatch")
    public ResponseEntity deleteBatch(@RequestBody List<Unit> unitList) {
        if (unitService.check(unitList)) {
            return ResponseEntity.fail("已被使用，无法删除");
        }

        unitService.deleteBatch(unitList);
        return ResponseEntity.success();

    }

    @ResponseBody
    @RequiresPermissions("unit:status")
    @RequestMapping("/unit/enableBatch")
    public ResponseEntity enableBatch(@RequestBody List<Unit> unitList) {
        unitService.enableBatch(unitList);
        return ResponseEntity.success();
    }

    @ResponseBody
    @RequiresPermissions("unit:status")
    @RequestMapping("/unit/disableBatch")
    public ResponseEntity disableBatch(@RequestBody List<Unit> unitList) {
        if (unitService.check(unitList)) {
            return ResponseEntity.fail("已被使用，无法停用");
        }

        unitService.disableBatch(unitList);
        return ResponseEntity.success();
    }

    @ResponseBody
    @RequestMapping("/unit/getAll")
    public ResponseData getAllNormal() {
        Company company = getCompany();
        List<Unit> list = unitService.listNormal(company.getId());
        return ResponseData.success(list);
    }

    @ResponseBody
    @RequestMapping("/unit/getById")
    public ResponseData getById(String id) {
        Unit unit = unitService.getById(id);
        if (unit == null){
            return ResponseData.fail("id不正确");
        }
        return ResponseData.success(unit);
    }
}
